/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package net.wicp.tams.common.others.test.quartz;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.DateBuilder.nextGivenSecondDate;

import java.util.Date;

import org.junit.Before;
import org.junit.Test;
import org.quartz.CronTrigger;
import org.quartz.DateBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SimpleExample {

	private Scheduler sched;

	@Before
	public void initCalss() throws SchedulerException {
		SchedulerFactory sf = new StdSchedulerFactory();
		sched = sf.getScheduler();
	}

	@Test
	public void first() throws Exception {
		JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
		// Trigger trigger=getnextMinute();
		// Trigger trigger = simpleTriggers();
		Trigger trigger = cronTrigger();
		sched.scheduleJob(job, trigger);
		sched.start();
		try {
			// wait 65 seconds to show job
			Thread.sleep(65L * 1000L);
			// executing...
		} catch (Exception e) {
			//
		}
		sched.shutdown(true);
	}

	private Trigger getnextMinute() {
		// 下一分钟 0秒
		Date runTime = evenMinuteDate(new Date());
		Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
		return trigger;
	}

	private Trigger simpleTriggers() {
		Date startTime = DateBuilder.nextGivenSecondDate(null, 15);
		// 执行一次
		SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1").startAt(startTime)
				.build();
		// 执行10次
		SimpleTrigger trigger2 = newTrigger().withIdentity("trigger3", "group1").startAt(startTime)
				.withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(10)).build();
		return trigger2;
	}

	private Trigger cronTrigger() {
		CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1")
				.withSchedule(cronSchedule("0/2 * * * * ?")).build();
		return trigger;
	}

	@Test
	public void JobStateAndParameters() throws Exception {
		// get a "nice round" time a few seconds in the future....
		Date startTime = nextGivenSecondDate(null, 10);
		// job1 will only run 5 times (at start time, plus 4 repeats), every 10 seconds
		JobDetail job1 = newJob(ColorJob.class).withIdentity("job1", "group1").build();
		SimpleTrigger trigger1 = newTrigger().withIdentity("trigger1", "group1").startAt(startTime)
				.withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(4)).build();
		// pass initialization parameters into the job
		job1.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Green");
		job1.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1);

		// schedule the job to run
		Date scheduleTime1 = sched.scheduleJob(job1, trigger1);
		log.info(job1.getKey() + " will run at: " + scheduleTime1 + " and repeat: " + trigger1.getRepeatCount()
				+ " times, every " + trigger1.getRepeatInterval() / 1000 + " seconds");
		sched.start();

		log.info("------- Waiting 60 seconds... -------------");
		try {
			// wait five minutes to show jobs
			Thread.sleep(60L * 1000L);
			// executing...
		} catch (Exception e) {
			//
		}
		sched.shutdown(true);
		log.info("------- Shutdown Complete -----------------");
		SchedulerMetaData metaData = sched.getMetaData();
		log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");

	}

}
